/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DaveSexton.DocProject.Configuration;

namespace DaveSexton.DocProject.ExternalUI
{
	public partial class OptionsDialog : Form
	{
		#region Public Properties
		public string SelectedBuildEngineProviderName
		{
			get
			{
				return (string) buildEngineProviderComboBox.SelectedItem;
			}
			set
			{
				if (value == null)
					throw new ArgumentNullException("value");

				buildEngineProviderComboBox.SelectedItem = value;
			}
		}
		#endregion

		#region Private / Protected
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="OptionsDialog" /> class.
		/// </summary>
		public OptionsDialog()
		{
			InitializeComponent();

			if (DocProjectEnvironment.BuildEngineProviders.Count > 0)
			{
				foreach (BuildEngineProvider provider in DocProjectEnvironment.BuildEngineProviders)
					buildEngineProviderComboBox.Items.Add(provider.Name);
			}
		}
		#endregion

		#region Methods
		private void SynchronizeResetItem(GridItem item)
		{
			bool canReset = false;

			if (item != null && item.GridItemType == GridItemType.Property)
			{
				GridItem parent = item.Parent;

				object obj = null;

				if (parent.GridItemType == GridItemType.Category || parent.GridItemType == GridItemType.Root)
					obj = grid.SelectedObject;
				else if (parent.GridItemType == GridItemType.Property)
					obj = parent.Value;

				canReset = obj != null && item.PropertyDescriptor.CanResetValue(obj)
					&& item.PropertyDescriptor.ShouldSerializeValue(obj);
			}

			resetToolStripMenuItem.Enabled = canReset;
		}
		#endregion

		#region Event Handlers
		private void buildEngineProviderComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			string item = (string) buildEngineProviderComboBox.SelectedItem;

			if (item != null)
				grid.SelectedObject = DocProjectEnvironment.BuildEngineProviders[item];
			else
				grid.SelectedObject = null;
		}

		private void grid_SelectedGridItemChanged(object sender, SelectedGridItemChangedEventArgs e)
		{
			SynchronizeResetItem(e.NewSelection);
		}

		private void grid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
		{
			GridItem item = e.ChangedItem;

			if (item == grid.SelectedGridItem)
				SynchronizeResetItem(item);
		}

		private void resetToolStripMenuItem_Click(object sender, EventArgs e)
		{
			grid.ResetSelectedProperty();
		}

		private void descriptionToolStripMenuItem_Click(object sender, EventArgs e)
		{
			grid.HelpVisible = descriptionToolStripMenuItem.Checked;
		}
		#endregion
	}
}